package com.jmb.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.lease.common.exception.LeaseException;
import com.jmb.lease.common.result.ResultCodeEnum;
import com.jmb.lease.model.entity.*;
import com.jmb.lease.model.enums.ItemType;
import com.jmb.lease.web.admin.mapper.*;
import com.jmb.lease.web.admin.service.ApartmentFacilityService;
import com.jmb.lease.web.admin.service.ApartmentFeeValueService;
import com.jmb.lease.web.admin.service.ApartmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.lease.web.admin.service.GraphInfoService;
import com.jmb.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.jmb.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.jmb.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.jmb.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.jmb.lease.web.admin.vo.fee.FeeValueVo;
import com.jmb.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService FacilityService;
    @Autowired
    private ApartmentLabelServiceImpl apartmentLabelService ;
    @Autowired
    private ApartmentFeeValueService feeValueService;

    @Autowired
    private ApartmentInfoMapper mapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;


    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 新增或者删除数据，我们需要判断数据是否存在,如果存在我们在进行更新，不存在我们进行新增
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate){
            // 删除图片列表
            LambdaQueryWrapper<GraphInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            wrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(wrapper);
            // 删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
            facilityWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            FacilityService.remove(facilityWrapper);
            // 删除标签列表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaWrapper);
            // 删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            feeValueService.remove(feeValueLambdaWrapper);
            // 插入图片列表
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            // 判断图片列表是否为空，不为空执行插入
            if (!CollectionUtils.isEmpty(graphVoList)){
                ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemType(ItemType.APARTMENT);
                    graphInfo.setItemId(apartmentSubmitVo.getId());
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfoList.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfoList);
            }
            // 新增配套列表
            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)){
                ArrayList<ApartmentFacility> FacilityList = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    ApartmentFacility apartmentFacility = new ApartmentFacility();
                    apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFacility.setFacilityId(facilityInfoId);
                    FacilityList.add(apartmentFacility);
                }
                FacilityService.saveBatch(FacilityList);
            }
            // 新增标签列表
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            if (!CollectionUtils.isEmpty(labelIds)){
                ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
                for (Long labelId : labelIds) {
                    ApartmentLabel apartmentLabel = new ApartmentLabel();
                    apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                    apartmentLabel.setLabelId(labelId);
                    apartmentLabelList.add(apartmentLabel);
                }
                apartmentLabelService.saveBatch(apartmentLabelList);
            }
            // 新增杂费列表
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            if (!CollectionUtils.isEmpty(feeValueIds)){
                ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
                for (Long feeValueId : feeValueIds) {
                    ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFeeValue.setFeeValueId(feeValueId);
                    apartmentFeeValueList.add(apartmentFeeValue);
                }
                feeValueService.saveBatch(apartmentFeeValueList);
            }
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return mapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null){
            return null;
        }
        // 查询图片列表
        List<GraphVo>  graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        // 查询配套列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        // 配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartemntId(id);

        ApartmentDetailVo detailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,detailVo);
        detailVo.setGraphVoList(graphVoList);
        detailVo.setLabelInfoList(labelInfoList);
        detailVo.setFacilityInfoList(facilityInfoList);
        detailVo.setFeeValueVoList(feeValueVoList);
       return detailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(roomInfoWrapper);
        if (count > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        // 1. 删除图片GraphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoWrapper);
        // 2. 删除标签ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelWrapper = new LambdaQueryWrapper<>();
        labelWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelWrapper);
        // 3. 删除配套ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
        facilityWrapper.eq(ApartmentFacility::getApartmentId,id);
        FacilityService.remove(facilityWrapper);
        // 4. 删除杂费ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        feeValueService.remove(feeValueWrapper);
        super.removeById(id);
    }
}




